// 抬头图片的显示与隐藏
$(function () {
    $(".btn").on("click", function () {
        $(".img-wrapper>.title").toggleClass("active");
        $(".img-wrapper>.btn").toggleClass("change");
    })
})


//百度模糊搜索
let search = document.getElementById("search");
let list = document.getElementById("list");
search.addEventListener("input", handlerJsonpRequest)
function handlerJsonpRequest() {

    var script = document.createElement("script");
    script.src = ` https://www.baidu.com/sugrec?prod=pc&from=pc_web&wd=${search.value}&cb=handlerJsonpResponse`
    document.body.appendChild(script);
    script.remove();
}
window.handlerJsonpResponse = function (data) {
    // console.log(data);
    list.style.display = "block"
    if (data.g === undefined) {
        list.innerHTML = ""
    } else {
        list.innerHTML = data.g.map(item => `<li>${item.q}</li>`).join("")
    }
}







// banner轮播图
var index = 0 ; 
function changeItem() {
    $(".slide").each(function (i) {
        $(this).removeClass("active");
        if(i==index){
            $(this).addClass("active");
        }
    })
    $(".page-item").each(function (i) {
        $(this).removeClass("active");
        if(i==index){
            $(this).addClass("active");
        }
    })
}

function handlerPrevClick(){
      if( index === 0 ){
            index = 7; 
      }else{
            index --;
      }
      changeItem();
}

function handlerNextClick(){
      if( index >= 7 ){
            index = 0; 
      }else{
            index ++;
      }
      changeItem();
}     

function autoPlay(){
      autoPlay.t = setInterval( function(){
            handlerNextClick()
      } ,  2000)
}     

function stopPlay(){
      clearInterval( autoPlay.t );
}

$(".banner").mouseenter(function(){
    stopPlay();
    $(".button-prev").css({
        display: "block"
    })
    $(".button-next").css({
        display: "block"
    })
});
$(".banner").mouseleave(function(){
    autoPlay();
    $(".button-prev").css({
        display: "none"
    })
    $(".button-next").css({
        display: "none"
    })
});

function handlerIndexOver(){
    index = $(this).attr("nav-index");
    changeItem();
}
autoPlay()

// 先取消之前添加的事件，防止事件叠加
// 给btn添加click事件
$(".button-prev").off("click").on("click", handlerPrevClick);
$(".button-next").off("click").on("click", handlerNextClick);
// 给banner-nav添加mouseover事件
$(".page-item").each(function () {
    $(this).on("mouseover", handlerIndexOver);
})
